<template>
  <!-- <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"> -->
    <div id="meswarehouseinfos-add-or-update">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
    <el-form-item label="仓库名称" prop="warehouseName">
      <el-input v-model="dataForm.warehouseName" placeholder="仓库名称"></el-input>
    </el-form-item>
    <el-form-item label="是否启用">
      <!-- <el-input v-model="dataForm.enabledFlg" placeholder="是否启用"></el-input> -->
      <el-select v-model="dataForm.enabledFlg" placeholder="是否启用">
        <el-option
          v-for="item in sysYesNo"
          :key="item.code"
          :label="item.name"
          :value="item.code">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="仓库位置">
      <el-input v-model="dataForm.warehouseSite" placeholder="仓库位置"></el-input>
    </el-form-item>
    <el-form-item label="仓库管理员">
      <el-input v-model="dataForm.warehouseKeeper" placeholder="仓库管理员"></el-input>
    </el-form-item>
    <el-form-item label="仓库类型">
      <!-- <el-input v-model="dataForm.warehouseType" placeholder="仓库类型"></el-input> -->
      <el-select v-model="dataForm.warehouseType" placeholder="仓库类型">
        <el-option
          v-for="item in warehouseType"
          :key="item.code"
          :label="item.name"
          :value="item.code">
        </el-option>
      </el-select>
    </el-form-item>
    </el-form>
    <div class="footer">
      <el-button type="primary" @click="dataFormSubmit()">保存</el-button>
      <el-button @click="cancle">取消</el-button>
    </div>
    </div>
  <!-- </el-dialog> -->
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        warehouseNum: '',
        warehouseName: '',
        warehouseSite: '',
        warehouseKeeper: '',
        warehouseType: '',
        enabledFlg: ''
      },
      dataRule: {
        warehouseName: [
          { required: true, message: '仓库名称不能为空', trigger: 'blur' }
        ],
        warehouseSite: [
          { required: true, message: '仓库位置不能为空', trigger: 'blur' }
        ],
        warehouseKeeper: [
          { required: true, message: '仓库管理员不能为空', trigger: 'blur' }
        ],
        warehouseType: [
          { required: true, message: '仓库类型不能为空', trigger: 'blur' }
        ]
      },
      warehouseType: [{ code: '', name: '' }],
      sysYesNo: []
    }
  },
  mounted() {
    this.optionLists()
    this.init(this.$route.params.id)
  },
  methods: {
    init(id) {
      this.dataForm.warehouseNum = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.warehouseNum) {
          this.$http({
            url: this.$http.adornUrl(
              `/basic/meswarehouseinfos/info/${this.dataForm.warehouseNum}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm = data.mesWarehouseInfoS
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/basic/meswarehouseinfos/${
                !this.dataForm.warehouseNum ? 'save' : 'update'
              }`
            ),
            method: 'post',
            data: this.$http.adornData({
              warehouseNum: this.dataForm.warehouseNum || undefined,
              warehouseName: this.dataForm.warehouseName,
              warehouseSite: this.dataForm.warehouseSite,
              warehouseKeeper: this.dataForm.warehouseKeeper,
              warehouseType: this.dataForm.warehouseType,
              enabledFlg: this.dataForm.enabledFlg
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  // this.visible = false
                  // this.$emit('refreshDataList')
                  eventBus.$emit('closeCurrentPage')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    // 取消
    cancle() {
      eventBus.$emit('closeCurrentPage')
    },
    optionLists() {
      let arr = ['warehouse_type', 'sys_yes_no']
      this.$http({
        url: this.$http.adornUrl('/basic/mescodemasterm/dataList'),
        method: 'post',
        data: this.$http.adornParams(arr, false)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          data.warehouse_type.forEach((item, index) => {
            let obj = {}
            obj.code = item.code
            obj.name = item.name
            this.warehouseType.push(obj)
          })
          data.sys_yes_no.forEach((item, index) => {
            let obj = {}
            obj.code = item.code
            obj.name = item.name
            this.sysYesNo.push(obj)
          })
        } else {
        }
        this.dataListLoading = false
      })
    }
  }
}
</script>
<style scoped lang="scss">
#meswarehouseinfos-add-or-update {
  width: 60%;
  margin: 0 auto;
  .el-form-item {
    .el-select {
      width: 500px;
    }
    .el-input {
      width: 500px;
    }
  }
  .footer {
    text-align: center;
  }
}
</style>